package code3;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Code implements Runnable{

    public void run(){

        String str1 = "F:/b";
        String str2 = "F:/new_b";
        Copy(str1, str2);
    }

    public static void Copy(String str1, String str2){

        File f1 = new File(str1);
        File f2 = new File(str2);
        f2.mkdir();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        int res = 0;
        byte[] arr = new byte[1024];
        File[] files = f1.listFiles();
        for(File f:files){
            String s = f.getName();
            String s1 = f.getAbsolutePath();
            String s2 = str2+"/"+s;
            if(f.isFile()){
                try{
                    bis = new BufferedInputStream(new FileInputStream(s1));
                    bos = new BufferedOutputStream(new FileOutputStream(s2));
                    while((res=bis.read(arr))!=-1){
                        bos.write(arr, 0, res);
                    }
                    System.out.println("文件"+s+"拷贝成功！");
                }catch(IOException e){
                    e.printStackTrace();
                }finally{
                    if(null!=bos){
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(null!=bis){
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                Copy(s1, s2);
                System.out.println("目录["+s+"]拷贝成功！");
            }
        }
    }

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.submit(new Code());
        executorService.shutdown();
    }
}
